package com.sanxiau.dormitory.yang.service.impl;

import com.sanxiau.dormitory.yang.entity.Leave;
import com.sanxiau.dormitory.yang.entity.LoginIn;
import com.sanxiau.dormitory.yang.entity.Student;
import com.sanxiau.dormitory.yang.face.Base64Util;
import com.sanxiau.dormitory.yang.face.FaceAdd;
import com.sanxiau.dormitory.yang.face.FaceDelete;
import com.sanxiau.dormitory.yang.mapper.AttendanceMapper;
import com.sanxiau.dormitory.yang.mapper.LeaveMapper;
import com.sanxiau.dormitory.yang.mapper.LoginMapper;
import com.sanxiau.dormitory.yang.mapper.StudentMessageMapper;
import com.sanxiau.dormitory.yang.service.LeaveService;
import com.sanxiau.dormitory.yang.service.StudentMessageService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;


@Service
public class StudentMessageServiceImpl implements StudentMessageService {
    @Autowired
    LeaveMapper leaveMapper;
    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    StudentMessageMapper studentMessageMapper;
    @Autowired
    LoginMapper loginMapper;

    @Override
    public Student showMessage(HttpSession session) {
        Student student = attendanceMapper.queryStudentByStu_id(session.getAttribute("userName")+"");
        //Student student = attendanceMapper.queryStudentByStu_id("201706024151");
        return student;
    }

    @Override
    public ResultInfo updateStudent(Student student) {
        ResultInfo resultInfo = new ResultInfo();
        int num = studentMessageMapper.updateStudent(student);
        if (num > 0){
            resultInfo.setMessage("修改成功");
        }else {
            resultInfo.setMessage("修改失败，请重试");
        }
        return resultInfo;
    }

    @Override
    public int addPhoto(byte[] photo, String stu_id) {
        LoginIn loginIn = loginMapper.queryUserByName(stu_id);
        FaceDelete.delete(loginIn.getUser_id()+"");
        FaceAdd.add(Base64Util.encode(photo),loginIn.getUser_id()+"");
        int num = studentMessageMapper.updatePhoto(photo,stu_id);
        return num;
    }

}
